<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/8
 * Time: 18:16
 */
namespace app\common\helper;

use app\common\model\AppmsgSend;

class appmsg_send_helper
{
    /**
     * 合并图文，生成新的图文
     * @param array $main_appmsg_list
     * @param array $extra_appmsg_list
     * @param int $store_id
     * @param int $send_task_id
     * @return int
     */
    public static function add_multi_appmsg_send($main_appmsg_list, $extra_appmsg_list = array(), $store_id = 0, $send_task_id = 0)
    {
        $new_appmsg_list = array_merge($main_appmsg_list, $extra_appmsg_list);

        //$appmsg_id = $main_appmsg_list[0]['id'];
        //an_dump($new_appmsg_list);

        // 根据数量确认是要生成单图文还是多图文
        $count = count($new_appmsg_list);
        $type = 1;
        if ($count > 1) {
            $type = 2;
        }

        // 多图文的merge_id
        $merge_id = 0;
        foreach ($new_appmsg_list as $k=>$v) {
            // 图文最多只有有8个，多的发不出去，所以不能生成超过8个的
            if ($k >= 8) {
                continue;
            }

            $tmp_appmsg_info = array(
                'send_task_id'  => $send_task_id,
                'appmsg_id'     => $v['id'],
                'store_id'      => $store_id,
                'merge_id'      => $merge_id,
                'title'         => $v['title'],
                'intro'         => $v['intro'],
                'cover'         => $v['cover'],
                'show_cover'    => $v['show_cover'],
                'link'          => $v['link'],
                'content'       => $v['content'],
                'type'          => $type,
                'content_type'  => $v['content_type'],
                'source_url'    => $v['source_url']
            );

            $new    = AppmsgSend::create($tmp_appmsg_info);
            $new_id = $new->id;
            //
            if ($k == 0 && $merge_id == 0) {
                $merge_id = $new_id;
            }
        }

        return $merge_id;
    }

    /**
     * 根据merge_id取得图文信息
     * @param int $merge_id
     * @return array
     */
    public static function get_appmsg_list($id, $need_empty = 1)
    {
        $main_info = _uri('appmsg_send', ['id'=>$id]);
        if (!$main_info || $main_info['status'] != 1) {
            $list = array();
            if ($need_empty) {
                $list[0] = appmsg_helper::get_empty_appmsg();
            }

            return $list;
        }

        if ($main_info['type'] == 1) {
            //$main_info['link'] = htmlspecialchars_decode($main_info['link']);
            return array($main_info);
        }

        if ($main_info['relation_id'] > 0) {
            $relation_info = _uri('appmsg_send', $main_info['relation_id']);
            if ($relation_info) {
                $main_info = appmsg_helper::merge_appmsg($main_info, $relation_info);
            }
        }

        $list = self::get_merge_appmsg_list($id, 1);
        array_unshift($list, $main_info);

        //         foreach ($list as $k=>$v) {
        //             $list[$k]['link'] = htmlspecialchars_decode($v['link']);
        //         }

        return $list;
    }


    /**
     * 根据merge_id取得图文信息
     * @param int $merge_id
     * @return array
     */
    public static function get_merge_appmsg_list($merge_id, $get_relation = 0)
    {
        $son_list = $tmp_son_list =  getList('appmsg_send', array('merge_id'=>$merge_id, 'status'=>1), [['view_order','id'=>'asc'],'']);
        // 如果需要处理子集
        if ($get_relation == 1) {
            $son_list = array();

            foreach ($tmp_son_list as $k=>$v) {
                if (!$v) {
                    continue;
                }

                $relation_info = array();
                if ($v['relation_id']) {
                    $relation_info = _uri('appmsg_send', ['id'=>$v['relation_id']]);
                    if (!$relation_info) {
                        continue;
                    }
                }

                $son_list[] = self::merge_appmsg($v, $relation_info);
            }
        }

        return $son_list;
    }

    /**
     * 合并关联的图文信息
     * @param array $info
     * @param array $relation_info
     * @return multitype:
     */
    public static function merge_appmsg($info, $relation_info)
    {
        if (!$relation_info) {
            return $info;
        }

        unset($relation_info['id']);
        unset($relation_info['category_id']);
        unset($relation_info['merge_id']);
        unset($relation_info['type']);
        unset($relation_info['relation_id']);
        unset($relation_info['view_order']);
        $relation_info['real_cover'] = _image($relation_info['cover'], 'small');

        return array_merge($info, $relation_info);
    }

}